﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Medical.Business;
using Medical.Entity;
using System.IO;

public partial class webadmin_SetAdver : AdminPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            InitData();
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.Adver;
    }

    protected void InitData()
    {
        DataTable dt = new BisBanner().GetList(AdverTypeEnum.HomeBanner);
        if (dt != null && dt.Rows.Count > 0)
        {
            hfHomeImg.Value = dt.Rows[0]["ImgPlace"].ToString();
            hfHomeID.Value = dt.Rows[0]["ID"].ToString();
            txtHomeName.Text = dt.Rows[0]["Name"].ToString();
            txtHomeLink.Text = dt.Rows[0]["Url"].ToString();
            imgHome.ImageUrl = "~/" + WebConfigInfo.FilePlace + "d/" + dt.Rows[0]["ImgPlace"].ToString();
        }

        //DataTable dt2 = new BisBanner().GetList(AdverTypeEnum.HomeBanner1);
        //if (dt2 != null && dt2.Rows.Count > 0)
        //{
        //    hfHomeImg1.Value = dt2.Rows[0]["ImgPlace"].ToString();
        //    hfHomeID1.Value = dt2.Rows[0]["ID"].ToString();
        //    txtHomeName1.Text = dt2.Rows[0]["Name"].ToString();
        //    txtHomeLink1.Text = dt2.Rows[0]["Url"].ToString();
        //    imgHome1.ImageUrl = "~/" + WebConfigInfo.FilePlace + "d/" + dt2.Rows[0]["ImgPlace"].ToString();
        //}

        DataTable dt1 = new BisBanner().GetList(AdverTypeEnum.ArticleBanner);
        if (dt1 != null && dt1.Rows.Count > 0)
        {
            hfArticleImg.Value = dt1.Rows[0]["ImgPlace"].ToString();
            hfArticleID.Value = dt1.Rows[0]["ID"].ToString();
            txtArticleName.Text = dt1.Rows[0]["Name"].ToString();
            txtArticleLink.Text = dt1.Rows[0]["Url"].ToString();
            imgArticle.ImageUrl = "~/" + WebConfigInfo.FilePlace + "d/" + dt1.Rows[0]["ImgPlace"].ToString();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        bool result = false;
        string strImg = string.Empty;
        //上传图片
        if (fuHomeImg.HasFile)
        {
            result = UpImg(fuHomeImg, ref strImg);
            if (!result)
                return;
        }
        else
        {
            Until.Alert("请选择要上传的图片！");
            return;
        }
        Banner Info = new Banner();
        Info.ID = Convert.ToInt32(hfHomeID.Value);
        Info.ImgPlace =(strImg == string.Empty ? hfHomeImg.Value : strImg);
        Info.Name =txtHomeName.Text.Trim();
        Info.Url = txtHomeLink.Text.Trim();
        result = new BisBanner().Update(Info);
        if (result)
        {
            if (!string.IsNullOrEmpty(strImg) && !string.IsNullOrEmpty(hfHomeImg.Value))
                Until.DeleteFile(Server.MapPath("~/" + WebConfigInfo.FilePlace + "d/" + hfHomeImg.Value));
        }
        Until.Alert(result ? "提交成功！" : "提交失败！");
        InitData();

    }

    //protected void btnSubmit2_Click(object sender, EventArgs e)
    //{
    //    bool result = false;
    //    string strImg = string.Empty;
    //    //上传图片
    //    if (fuHomeImg1.HasFile)
    //    {
    //        result = UpImg(fuHomeImg1, ref strImg);
    //        if (!result)
    //            return;
    //    }
    //    else
    //    {
    //        Until.Alert("请选择要上传的图片！");
    //        return;
    //    }
    //    Banner Info = new Banner();
    //    Info.ID = Convert.ToInt32(hfHomeID1.Value);
    //    Info.ImgPlace = (strImg == string.Empty ? hfHomeImg1.Value : strImg);
    //    Info.Name = txtHomeName1.Text.Trim();
    //    Info.Url = txtHomeLink1.Text.Trim();
    //    result = new BisBanner().Update(Info);
    //    if (result)
    //    {
    //        if (!string.IsNullOrEmpty(strImg) && !string.IsNullOrEmpty(hfHomeImg1.Value))
    //            Until.DeleteFile(Server.MapPath("~/" + WebConfigInfo.FilePlace + "d/" + hfHomeImg1.Value));
    //    }
    //    Until.Alert(result ? "提交成功！" : "提交失败！");
    //    InitData();

    //}

    /// <summary>
    /// 上传图片
    /// </summary>
    /// <param name="fileImg"></param>
    /// <param name="strImg"></param>
    /// <returns></returns>
    protected bool UpImg(FileUpload fileImg, ref string strImg)
    {
        string strExtension = Path.GetExtension(fileImg.FileName).ToLower();
        if (fileImg.PostedFile.ContentLength > WebConfigInfo.FileSize)
        {
            Until.Alert("图片大小不超过2M！");
            return false;
        }
        //验证图片格式
        if (Until.CheckFileType(0, strExtension))
        {
            string strFileName = Guid.NewGuid().ToString().Replace("-", "") + strExtension;
            string strYear = DateTime.Now.Year.ToString();
            string strMonth = DateTime.Now.Month.ToString();

            //原图
            string strFile = Server.MapPath("~/" + WebConfigInfo.FilePlace + "/d/" + strYear + "/" + strMonth + "/");
            if (!Directory.Exists(strFile))
            {
                Directory.CreateDirectory(strFile);
            }
            fileImg.SaveAs(strFile + strFileName);

            strImg = strYear + "/" + strMonth + "/" + strFileName;
            return true;
        }
        else
        {
            Until.Alert("图片格式不正确！");
            return false;
        }
    }


    protected void btnSubmit1_Click(object sender, EventArgs e)
    {
        bool result = false;
        string strImg = string.Empty;
        //上传图片
        if (fuArticleImg.HasFile)
        {
            result = UpImg(fuArticleImg, ref strImg);
            if (!result)
                return;
        }
        Banner Info = new Banner();
        Info.ID = Convert.ToInt32(hfArticleID.Value);
        Info.ImgPlace = strImg==string.Empty?hfArticleImg.Value:strImg;
        Info.Name = txtArticleName.Text.Trim();
        Info.Url = txtArticleLink.Text.Trim();
        result = new BisBanner().Update(Info);
        if (result)
        {
            if (!string.IsNullOrEmpty(strImg)&&!string.IsNullOrEmpty(hfArticleImg.Value))
                Until.DeleteFile(Server.MapPath("~/" + WebConfigInfo.FilePlace + "d/" + hfArticleImg.Value));
        }
        Until.Alert(result ? "提交成功！" : "提交失败！");
        InitData();
    }
}